﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBService;
using System.Drawing;
using DBService.ViewDatas;
using System.IO;
public partial class AddImages : System.Web.UI.Page
{
    int UserID;
    ModelAdapter ma = new ModelAdapter();
    UserInfo u;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (int.TryParse(Request.QueryString["id"], out UserID))
        {
            u = ma.GetUser(UserID);
            if (!IsPostBack)
            {
                LitImage.Text = "<img id='mainimg' src='../upload/" + u.Avatar + "' alt='Image To Crop' />";
            }
        }
    }

    protected void btnCropIt_Click(object s, EventArgs e)
    {
        FileStream fs = new FileStream(Server.MapPath("../upload/" + u.Avatar),FileMode.Open);

        System.Drawing.Image img = System.Drawing.Image.FromStream(fs);//.FromFile(Server.MapPath("../upload/" + u.Avatar));
        var m = cropImage(img, new Rectangle(0, 0, 50, 50));
        fs.Close();
        System.IO.File.Delete(Server.MapPath("../upload/" + u.Avatar));
        m.Save(Server.MapPath("../upload/" + u.Avatar));
    }


    private static System.Drawing.Image cropImage(System.Drawing.Image img, Rectangle cropArea)
    {
        Bitmap bmpImage = new Bitmap(img);
        Bitmap bmpCrop = bmpImage.Clone(cropArea, bmpImage.PixelFormat);
        return (System.Drawing.Image)(bmpCrop);
    }
}